package model

import (
	"aglaia/pkg/util"
	"crypto/sha256"
	"github.com/jinzhu/gorm"
)

type Auth struct {
	Model

	AppKey    string `json:"app_key"`
	AppSecret string `json:"-"`

	Name   string `json:"name"`
	Avatar string `json:"avatar"`

	//PrivateKey  string `json:"-" gorm:"type:text"`
}

func (a Auth) TableName() string {
	return "auth"
}

//var (
//	ErrAppSecret = errors.New("app secret error")
//)

func (a *Auth) Get(tx *gorm.DB) error {
	if err := tx.Set("gorm:query_option", "FOR UPDATE").
		Where("app_key = ? and app_secret = ?", a.AppKey, util.Encrypt(a.AppSecret, sha256.New())).First(a).Error; err != nil {
		return err
	}

	return nil
}

func (a *Auth) QueryById(tx *gorm.DB) error {
	return tx.Where("id = ?", a.Id).First(a).Error
}
